/**
 * @Author：ming
 * @Date：2023/10/9 2:38 AM
 * @Filename：Lc739
 */

import java.util.Stack;

/**
 * @author ming
 */
public class Lc739 {
    public int[] dailyTemperatures(int[] temperatures) {
        int len = temperatures.length;
        int[] high = new int[len];
        Stack<Integer> stack = new Stack<>();
        Stack<Integer> index = new Stack<>();
        stack.push(temperatures[0]);
        index.push(0);

        for (int i = 1; i < len; i++) {
            if(temperatures[i]>stack.peek()){
                while (!stack.isEmpty() && temperatures[i]>stack.peek()){
                    high[index.peek()]=i-index.peek();
                    index.pop();
                    stack.pop();
                }

            }
            stack.push(temperatures[i]);
            index.push(i);
        }
        return high;
    }
}
